package com.hc.java.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 线程池invoke会阻塞
 *
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/6/17 11:38
 */
public class 线程池invoke {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = new ThreadPoolExecutor(2, 2,
                5, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20), r -> {
            Thread thread = new Thread(r);
            thread.setName("线程池invoke-" + thread.hashCode());
            return thread;
        });
        List<Callable<Integer>> list = new ArrayList<Callable<Integer>>() {{
            add(() -> {
                System.out.println("线程执行中------1");
                Thread.sleep(1000);
                System.out.println("线程执行完了------2");
                return 1;
            });
        }};
        List<Future<Integer>> futures = executor.invokeAll(list);
        System.out.println("等待线程执行--------3");
        for (Future<Integer> f : futures) {
            System.out.println("执行结果4：" + f.get());
        }
        System.exit(0);
    }
}
